﻿using Grpc.Net.Client;
using Protos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.Business.Tasks
{
    public class QuartzService : IQuartzService
    {
        async Task IQuartzService.InitJobsAsync()
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.InitJobsAsync(new Google.Protobuf.WellKnownTypes.Empty());
                Console.WriteLine($"结果:{reply}");
            }
        }
        async Task<List<ResponseJob.Types.JobsModel>> IQuartzService.GetJobs() 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.GetJobsAsync(new Google.Protobuf.WellKnownTypes.Empty());
                return reply.Data.ToList();
            }
        }
        async Task<bool> IQuartzService.AddJob(ResponseJob.Types.JobsModel request) 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.AddJobAsync(request);
                return reply.Value;
            }
        }
        async Task<bool> IQuartzService.Remove(ResponseJob.Types.JobsModel request) 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.RemoveAsync(request);
                return reply.Value;
            }
        }
        async Task<bool> IQuartzService.Update(ResponseJob.Types.JobsModel request) 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.UpdateAsync(request);
                return reply.Value;
            }
        }
        async Task<bool> IQuartzService.Pause(ResponseJob.Types.JobsModel request) 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.PauseAsync(request);
                return reply.Value;
            }
        }
        async Task<bool> IQuartzService.Start(TaskRequest request) 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.StartAsync(request);
                return reply.Value;
            }
        }
        async Task<bool> IQuartzService.Run(ResponseJob.Types.JobsModel request) 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.RunAsync(request);
                return reply.Value;
            }
        }
        async Task<string> IQuartzService.GetState(ResponseJob.Types.JobsModel request) 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.GetStateAsync(request);
                return reply.Value;
            }
        }
        async Task<bool> IQuartzService.IsQuartzJob(TaskRequest request) 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = await client.IsQuartzJobAsync(request);
                return reply.Value;
            }
        }
        bool IQuartzService.IsValidExpression(string request) 
        {
            //常规使用，https
            //string url = "https://localhost:5260";

            //使用http
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string url = "http://localhost:5260";

            using (var channel = GrpcChannel.ForAddress(url))
            {
                var client = new Quartz.QuartzClient(channel);
                var reply = client.IsValidExpression(new ResponseString() { Value = request});
                return reply.Value;
            }
        }

        //Task<List<TaskLog>> IQuartzService.GetLog(long jobId, int page, int pageSize) 
        //{
        //    //常规使用，https
        //    //string url = "https://localhost:5260";

        //    //使用http
        //    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
        //    string url = "http://localhost:5260";

        //    using (var channel = GrpcChannel.ForAddress(url))
        //    {
        //        var client = new Quartz.QuartzClient(channel);
        //        var reply = client.get (new ResponseString() { Value = request });
        //        return reply.Value;
        //    }
        //}
    }
}
